package com.dcbeida.activity.clerkmanager;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.Customer;
import com.dcbeida.bean.Customers;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class ClerkStatusPresenter implements ClerkStatusContract.Present {


    private ClerkStatusContract.View view;

    private ModelUtils model;

    private Activity act;

    public ClerkStatusPresenter(ClerkStatusContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = ModelUtils.newInstance();
        this.act = act;

    }


    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };




    @Override
    public void modifyuserstatus(int status, String employeeid) {
        model.getDate("modifyuserstatus", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .putopt("status", status)
                .putopt("employeeid", employeeid)
                .tostring(), new CustomerStatusCallback(act,
                Object.class, lis,status));
    }

    @Override
    public void getemployeeinfo() {
        model.getDate("getemployeeinfo", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .tostring(), new CustomerStatusCallback(act,
                Customer.class, lis,0));
    }


    final class CustomerStatusCallback<T> extends DialogCallback<T> {

        private int status;
        public CustomerStatusCallback(Activity activity, Type type, DialogInterface.OnCancelListener listener,int status) {
            super(activity, type, listener);
            this.status=status;
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if(data instanceof Customer){
                    view.setCustomer((Customer) data);
                }else if(data instanceof Object) {
                    view.setSuccess(status);
                }
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");

        }
    }


}
